﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using XbsWebAPI.Application;
using XbsWebAPI.Application.SystemAdmin;
using XbsWebAPI.Application.SystemAdmin.Dtos;
using XbsWebAPI.Core.Repositories;
using XbsWebAPI.EntityFrameworkCore.Entities.SystemAdmin;
using XbsWebAPI.MainHost.ViewModels;

namespace XbsWebAPI.MainHost.Controllers
{
    [ApiController]
    [Route("api/xbs_v1/[controller]")]
    public class SystemRoleController : Controller
    {
        private readonly ISystemRoleAppService _roleAppService;

        public SystemRoleController(ISystemRoleAppService roleAppService)
        {
            _roleAppService = roleAppService;
        }

        /// <summary>
        /// 菜单列表树，展示所有的父子菜单关系（"identifying": 0-Select, 1-Add, 2-Edit, 3-Delete）
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public ResultDto<List<MenuTreeDto>> ShowMenusTree()
        {
            var result = _roleAppService.ShowMenusTree();
            return result;
        }

        /// <summary>
        /// 查询所有角色列表
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public ResultDto<List<ListSystemRoleDto>> FindAllSystemRoles()
        {
            var result = _roleAppService.GetSystemRoles();
            return result;
        }

        /// <summary>
        /// 查询单个角色
        /// </summary>
        /// <param name="id">角色Id</param>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public ResultDto<GetSystemRoleDto> FindSystemRoleById(Guid id)
        {
            var result = _roleAppService.GetSystemRoleById(id);
            return result;
        }

        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("[action]")]
        public async Task<ResultDto<string>> AddSystemRole(CreateSystemRoleDto model)
        {
            var result = await _roleAppService.CreateSystemRoleAsync(model);
            
            return result;
        }

        /// <summary>
        /// 编辑角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("[action]")]
        public async Task<ResultDto<string>> EditSystemRole(EditSystemRoleDto model)
        {
            var result = await _roleAppService.UpdateSystemRoleAsync(model);

            return result;
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id">角色Id</param>
        /// <returns></returns>
        [HttpGet, Route("[action]")]
        public async Task<ResultDto<string>> DeleteSystemRoleById(Guid id)
        {
            var result = await _roleAppService.RemoveRoleByIdAsync(id);

            return result;
        }

    }
}
